//
//  DatabaseManager.m
//  Apps
//
//  Created by SuJiang on 2018/4/5.
//  Copyright © 2018年 appscomm. All rights reserved.
//

#import "DatabaseManager.h"
#import <LYDatabase.h>
#import "Sport.h"
#import "SleepPoint.h"
#import "HeartRate.h"
#import "Sleep.h"
#import "DownloadRecord.h"
#import "Reminder.h"
#import "Firmware.h"
#import "FavoriteApp.h"
#import "Weight.h"
#import "Water.h"
#import "SportRecord.h"
#import "WorkoutMonthCount.h"
#import "WorkoutLocationPoint.h"
#import "LYDataBaseMigrateManager.h"
#import "DatabaseMigrateStep1.h"
#import "DatabaseMigrateStep2.h"

@interface DatabaseManager ()<LYDataBaseMigrateManagerDelegate>

@end

@implementation DatabaseManager

@synthesize databaseMigrateSteps = _databaseMigrateSteps;

+ (instancetype) sharedInstance
{
    static DatabaseManager *instance;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[DatabaseManager alloc] init];
    });
    return instance;
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self initDatabase];
    }
    return self;
}

- (void) initDatabase
{
    [LYDatabase database:@"Appscomm.sqlite"];
    [LYDatabase database].logOn = NO;
}

- (void) createTables
{
    NSArray <NSString *>*allTableNames = [[LYDatabase database] allTables];
    // 不是新安装的App，而是曾经有安装过的旧版本。
    if (allTableNames.count > 0) {
        
    }
    else {
        // 新安装的App，设置初始版本为最新版本
        LYDataBaseMigrateManager *migrater = [[LYDataBaseMigrateManager alloc] initWithDatabase:[LYDatabase database] delegate:self];
        if (!self.databaseMigrateSteps.count) {
            [migrater setDBVersion:0];
        }
        else {
            [migrater setDBVersion:[self.databaseMigrateSteps lastObject].completeVersion];
        }
    }
    
    [Sport createTable];
    [SleepPoint createTable];
    [HeartRate createTable];
    [Sleep createTable];
    [DownloadRecord createTable];
    [Reminder createTable];
    [Firmware createTable];
    [FavoriteApp createTable];
    [Weight createTable];
    [Water createTable];
    [SportRecord createTable];
    [SportRecordExercise createTable];
    [WorkoutMonthCount createTable];
    [WorkoutLocationPoint createTable];
}

- (void) migrateTablesWithProgress:(void(^)(NSProgress *progress))progress completion:(void(^)(void))completion
{
    LYDataBaseMigrateManager *migrater = [[LYDataBaseMigrateManager alloc] initWithDatabase:[LYDatabase database] delegate:self];
    [migrater startAsyncMigrateWithProgressHandler:^(NSProgress * _Nonnull pro) {
        NSLog(@"totalPro = %@",pro.localizedDescription);
        
        dispatch_async(dispatch_get_main_queue(), ^{
            if (progress) {
                progress(pro);
            }
        });
        
    } completion:^{
        dispatch_async(dispatch_get_main_queue(), ^{
            if (completion) {
                completion();
            }
        });
    }];
}

- (BOOL)needMigrateData {
    LYDataBaseMigrateManager *migrater = [[LYDataBaseMigrateManager alloc] initWithDatabase:[LYDatabase database] delegate:self];
    return [[self.databaseMigrateSteps lastObject] completeVersion] > [migrater DBVersion];
}

#pragma mark -LYDataBaseMigrateManagerDelegate
- (NSArray < id<LYDBMigrateStepProtocol>> *) databaseMigrateSteps {
    
    if (!_databaseMigrateSteps) {
        DatabaseMigrateStep1 *step1 = [[DatabaseMigrateStep1 alloc] init];
        DatabaseMigrateStep2 *step2 = [[DatabaseMigrateStep2 alloc] init];
        _databaseMigrateSteps = @[step1, step2];
    }
    return _databaseMigrateSteps;
}

@end
